package edu.drexel.cs.eVoter.VoterClient;

import java.util.Iterator;
import java.util.Vector;

//import javax.swing.JLabel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.jgroups.View;

import org.jgroups.Address;

public class DebugDataPanel extends ClientAbstractPanel 
	implements DebugListener
{
	private static final long serialVersionUID = -3140213572466230741L;
	private JTabbedPane jtp;
	private JTable      voterViewTable;
	private JTable      electionViewTable;
	
	private Vector<String> candidateColumns;
	private Vector<String> viewColumns;
	private Vector<Vector<String>> voterRowData;
	private Vector<Vector<String>> electionRowData;
	private Vector<Vector<String>> hashRowData;
	
	public DebugDataPanel(ServerInterface si) 
	{
		super(si);
		si.addDebugListener(this);
		
		candidateColumns = new Vector<String>();
		candidateColumns.add( "Candidates" );
		candidateColumns.add( "Votes" );
		viewColumns = new Vector<String>();
		viewColumns.add( "JGroups Address" );
		
		View v;
		if ( (v = myServerInterface.getRPCView()) == null  )
		{
			voterRowData = new Vector<Vector<String>>();
			voterRowData.add( new Vector<String>(1) );
			voterRowData.add( new Vector<String>(1) );
			voterRowData.add( new Vector<String>(1) );
			voterRowData.get(0).add( "No Data" );
			voterRowData.get(1).add( "No Data" );
			voterRowData.get(2).add( "No Data" );
		}
		else
		{
			voterRowData = new Vector<Vector<String>>();
			Iterator<Address> iter = v.iterator();
			
			while ( iter.hasNext() )
			{
				Vector<String> temp = new Vector<String>(1);
				temp.add( iter.next().toString() );
				voterRowData.add( temp );
			}
		}

		
		electionRowData = new Vector<Vector<String>>();
		electionRowData.add( new Vector<String>(1) );
		electionRowData.add( new Vector<String>(1) );
		electionRowData.add( new Vector<String>(1) );
		electionRowData.get(0).add( "No Data" );
		electionRowData.get(1).add( "No Data" );
		electionRowData.get(2).add( "No Data" );
		
		hashRowData = new Vector<Vector<String>>();
		hashRowData.add( new Vector<String>(2) );
		hashRowData.add( new Vector<String>(2) );
		hashRowData.add( new Vector<String>(2) );
		hashRowData.get(0).add( "No Data" );
		hashRowData.get(0).add( "0" );
		hashRowData.get(1).add( "No Data" );
		hashRowData.get(1).add( "No Data" );
		hashRowData.get(2).add( "No Data" );
		hashRowData.get(2).add( "0" );
		
		voterViewTable    = new JTable(voterRowData, viewColumns );
		electionViewTable = new JTable( electionRowData, viewColumns );

		jtp = new JTabbedPane();
		
		JScrollPane jsp;
		jsp = new JScrollPane( voterViewTable );
		
		jtp.addTab("Voter View", jsp );
		
		//jsp = new JScrollPane( electionViewTable );
		jsp = new JScrollPane( new JLabel("UNIMPLEMENTED - Correct data can be found in other tabs.") );
		
		jtp.addTab("Election View", jsp );
		
		//jsp = new JScrollPane( hashViewTable );
		jsp = new JScrollPane( new JLabel("UNIMPLEMENTED - Correct data can be found in other tabs.") );

		jtp.addTab("Internal Hash View", jsp );
		
		add(jtp);
	}

	protected void updateVoterView( View view )
	{
		voterRowData.clear();
		
		Iterator<Address> iter = view.iterator();
		
		while ( iter.hasNext() )
		{
			Vector<String> temp = new Vector<String>(1);
			temp.add( iter.next().toString() );
			voterRowData.add( temp );
		}
		
		voterViewTable.setModel( new DefaultTableModel(voterRowData, viewColumns ) );
	}
	
	protected void updateElectionView( View view )
	{
		electionRowData.clear();
		
		Iterator<Address> iter = view.iterator();
		
		while ( iter.hasNext() )
		{
			Vector<String> temp = new Vector<String>(1);
			temp.add( iter.next().toString() );
			electionRowData.add( temp );
		}
		
		electionViewTable.setModel( new DefaultTableModel(electionRowData, viewColumns ) );
	}


	@Override
	public void updateView(String channel, View newView) 
	{
		//System.out.println("is this occuring???? " + channel );
		if ( channel.compareTo(myServerInterface.channel_name) == 0 )
		{
			updateVoterView(newView);
		}
		else if ( channel.compareTo("Election") == 0 )
		{
			updateElectionView(newView);
		}
	}
}
